package painting;

import directory.SeeDirectory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GenerateBaJiImage {
    // 创建 BufferedImage 对象
    static BufferedImage image;
    static Graphics2D a4;
    //A4宽高
    static int width = 0;
    static int height = 0;
    //圆的行列
    static int cols = 0;
    static int rows = 0;
    // 计算圆之间的间距
    static int horizontalSpacing = 0;
    static int verticalSpacing = 0;
    // 设置圆的直径为 7厘米
    static int circleDiameter = (int) Math.round(7 * 300 / 2.54); // 转换 cm 到像素
    static int innerCircleDiameter = (int) Math.round(6 * 300 / 2.54);

    public static BufferedImage getImage(String[] images){
        a4 = GenerateA4();
        BufferedImage[] BaJiImages = BaJiImages(images);
        putBaJiImageToA4(BaJiImages);
        return image;
    }

    public static Graphics2D GenerateA4(){
        // A4 页面的尺寸，以像素为单位，300dpi 分辨率下的尺寸
        width = (int) Math.round(21 * 300 / 2.54); // A4 纸的宽度，转换 cm 到像素
        height = (int) Math.round(29.7 * 300 / 2.54); // A4 纸的高度，转换 cm 到像素

        // 创建 BufferedImage 对象
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 获取 Graphics2D 上下文
        Graphics2D g2d = image.createGraphics();

        // 设置背景颜色为白色
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        return g2d;
    }

    public static BufferedImage[] BaJiImages(String[] images){

        // 计算 A4 纸上可以容纳的圆的行数和列数
        cols = width / circleDiameter;
        rows = height / circleDiameter;

        // 计算圆之间的间距
        horizontalSpacing = (width - (cols * circleDiameter)) / (cols + 1);
        verticalSpacing = (height - (rows * circleDiameter)) / (rows + 1);

        // 加载要导入的图片
        BufferedImage[] circleImages = new BufferedImage[cols * rows];

        try {
            for (int i = 0; i < circleImages.length; i++) {
                circleImages[i] = ImageIO.read(new File(images[i])); // 修改为你要导入的图片文件路径
            }
        } catch (Exception e) {
            System.out.println("加载图片时出错：" + e.getMessage());
        }
        return circleImages;
    }

    public static void putBaJiImageToA4(BufferedImage[] BaJiImages){
        // 绘制圆和导入的图片
        a4.setColor(Color.DARK_GRAY);
        int imageIndex = 0; // 图片索引，用于选择图片
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                int x = (j * circleDiameter) + ((j + 1) * horizontalSpacing);
                int y = (i * circleDiameter) + ((i + 1) * verticalSpacing);

                // 计算内部同心圆的位置
                int innerX = x + (circleDiameter - innerCircleDiameter) / 2;
                int innerY = y + (circleDiameter - innerCircleDiameter) / 2;

                BufferedImage circleImage = BaJiImages[imageIndex];

                if (circleImage != null) {
                    // 缩放图片以适应圆的直径
                    BufferedImage scaledImage = new BufferedImage(innerCircleDiameter, innerCircleDiameter, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g = scaledImage.createGraphics();
                    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                    g.drawImage(circleImage, 0, 0, innerCircleDiameter, innerCircleDiameter, null);
                    g.dispose();

                    // 在圆的中心位置绘制图片
                    int imageX = innerX + (innerCircleDiameter - scaledImage.getWidth()) / 2;
                    int imageY = innerY + (innerCircleDiameter - scaledImage.getHeight()) / 2;
                    a4.setClip(new Ellipse2D.Float(innerX, innerY, innerCircleDiameter, innerCircleDiameter));
                    a4.drawImage(scaledImage, imageX, imageY, null);
                    a4.setClip(null);
                }

                // 绘制圆的轮廓
                //a4.drawOval(innerX, innerY, innerCircleDiameter, innerCircleDiameter);
                a4.drawOval(x, y, circleDiameter, circleDiameter);

                imageIndex++;
                System.out.println("============小李编写============");
            }
        }

        // 释放 Graphics2D 上下文
        a4.dispose();

    }
}
